package org.parademo.database;

import org.parademo.model.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author Ajit Skanda
 * @version 1.0
 */
public class DataStorageImpl implements DataStorage {

	public HashMap<String, ProposalEntity> proposals = null;
	public HashMap<String, MemberEntity> users = null;
	public HashMap<String, CommunityEntity> communities = null;

	public DataStorageImpl() {
		proposals = new HashMap<String, ProposalEntity>();
		users = new HashMap<String, MemberEntity>();
		communities = new HashMap<String, CommunityEntity>();

		createCommunities();
		createUsers();
	}

	public MemberEntity getUser(String userName) {
		return null;
	}

	public List<MemberEntity> getUsers(ArrayList<String> userIds) {
		return null;
	}

	public ProposalEntity createProposal(ProposalEntity proposalToCreate) {
		return null;
	}

	public ProposalEntity updateProposal(ProposalEntity proposalToUpdate) {
		return null;
	}

	public void deleteProposal(ProposalEntity proposalToDelete) {
	}

	public ProposalEntity getProposal(Long proposalId) {
		return null;
	}

	public List<ProposalEntity> getProposalsForIDs(ArrayList<Long> proposalIds) {
		return null;
	}

	public void castVote(VoteEntity vote) {
	}

	public void removeVote(VoteEntity vote) {
	}

	public void delegateVote(VoteEntity vote) {
	}

	public HashMap<String, CommunityEntity> getAllCommunities() {
		return communities;
	}

	public void setAllCommunities(HashMap<String, CommunityEntity> pCommunities) {
		communities = pCommunities;
	}

	public HashMap<String, ProposalEntity> getAllProposals() {
		return proposals;
	}

	public void setAllProposals(HashMap<String, ProposalEntity> pProposals) {
		proposals = pProposals;
	}

	public HashMap<String, MemberEntity> getAllUsers() {
		return users;
	}

	public void setAllUsers(HashMap<String, MemberEntity> pUsers) {
		users = pUsers;
	}

	/**
	 * Create the necessary Entity objects.
	 */
	private void createUsers() {

		//10 users belonging to each community
		CommunityEntity community = communities.get("Amsterdam");
		for (int i=0; i< 10; i++) {
			users.put("user"+i , new MemberEntity("user"+i, "passwd"+i, "firstName"+i, "lastName"+i, new Date(),
				new Date(),community));
		}

		community = communities.get("Osdorp");
		for (int i=10; i< 20; i++) {
			users.put("user"+i , new MemberEntity("user"+i, "passwd"+i, "firstName"+i, "lastName"+i, new Date(),
				new Date(),community));
		}

		community = communities.get("Amsterdam Zuid");
		for (int i=20; i< 30; i++) {
			users.put("user"+i , new MemberEntity("user"+i, "passwd"+i, "firstName"+i, "lastName"+i, new Date(),
				new Date(),community));
		}

		community = communities.get("Amsterdam Bijlmer");
		for (int i=30; i< 40; i++) {
			users.put("user"+i , new MemberEntity("user"+i, "passwd"+i, "firstName"+i, "lastName"+i, new Date(),
				new Date(),community));
		}

		community = communities.get("Amstelveen");
		for (int i=40; i< 50; i++) {
			users.put("user"+i , new MemberEntity("user"+i, "passwd"+i, "firstName"+i, "lastName"+i, new Date(),
				new Date(),community));
		}
	}

	private void createCommunities() {
		RegionEntity northHolandRegion = new RegionEntity("North Holland", new BigDecimal("52.22"),
				new BigDecimal("04.53"));
		CommunityEntity rootCommunity = new CommunityEntity("Amsterdam",null, northHolandRegion);
		communities.put(rootCommunity.getName(), rootCommunity);

		CommunityEntity osdorpCommunity = new CommunityEntity("Osdorp",rootCommunity, northHolandRegion);
		communities.put(osdorpCommunity.getName(), osdorpCommunity);

		CommunityEntity zuidCommunity = new CommunityEntity("Amsterdam Zuid",rootCommunity, northHolandRegion);
		communities.put(zuidCommunity.getName(), zuidCommunity);

		CommunityEntity bijlmerCommunity = new CommunityEntity("Amsterdam Bijlmer",rootCommunity, northHolandRegion);
		communities.put(bijlmerCommunity.getName(), bijlmerCommunity);

		CommunityEntity amstelveenCommunity = new CommunityEntity("Amstelveen",rootCommunity, northHolandRegion);
		communities.put(amstelveenCommunity.getName(), amstelveenCommunity);
	}

	private void createProposals() {
		//proposals.add(new ProposalEntity("proposal1", "Test proposal 1", ));

	}
}
